package com.ikangtai.shecare.common.baseView;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.util.AppLog;

public class CustomProgressDialog extends Dialog {
    private Context context = null;
    private CustomProgressDialog customProgressDialog = null;

    public CustomProgressDialog(Context context){
        super(context);
        this.context = context;
    }

    public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    public CustomProgressDialog createDialog(Context context){
        customProgressDialog = new CustomProgressDialog(context, R.style.CustomProgressDialog);
        customProgressDialog.setContentView(R.layout.custom_progress_dialog);
        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

        return customProgressDialog;
    }

    public void onWindowFocusChanged(boolean hasFocus){

        if (customProgressDialog == null){
            return;
        }

        ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);
        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
        animationDrawable.start();
    }

    public void showProgressDialog() {
        AppLog.i("customProgressDialog showProgressDialog! name = " + context.toString());
        createDialog(context);
        customProgressDialog.setCancelable(false);
        customProgressDialog.show();
    }

    public void showProgressDialog(Context context, String message) {
        createDialog(context);
        setMessage(message);
        customProgressDialog.setCancelable(false);
        customProgressDialog.show();
    }

    private void setMessage(String message){
        TextView loadingTextView = (TextView) customProgressDialog.findViewById(R.id.tipTextView);
        loadingTextView.setText(message);
    }

    public void dismissProgressDialog(){
        if (customProgressDialog != null && customProgressDialog.isShowing()) {
            AppLog.i("customProgressDialog is NOT NULL! name = " + context.toString());
            customProgressDialog.dismiss();
            customProgressDialog = null;
        }else
            AppLog.i("customProgressDialog is NULL! name = " + context.toString());
    }
}
